package com.xjrsoft.module.customerTwo.ycode;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.core.secure.XjrUser;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.entity.TYcodeClass;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.service.ITYcodeClassService;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.dto.TYcodeProductListDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.entity.TYcodeProduct;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.service.ITYcodeProductService;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.vo.TYcodeProductListVo;
import com.xjrsoft.module.customerTwo.ycode.vo.YcodeClassVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/yecode/no_login")
@Api(value = "分类管理", tags = "分类管理接口")
public class SkipLoginInterceptionController {
    @Autowired
    private ITYcodeClassService ycodeClassService;
    @Autowired
    private final ITYcodeProductService tYcodeProductService;


    @GetMapping("/getYcodeClass")
    @ApiOperation(value = "获取分类")
    public Response<List<YcodeClassVo>> getYcodeClass() {
        List<YcodeClassVo> result = new ArrayList<>();
        List<TYcodeClass> list = ycodeClassService.getSecondaryClassList();
        if(CollectionUtil.isNotEmpty(list)){
            for(TYcodeClass one : list){
                YcodeClassVo vo = BeanUtil.copy(one,YcodeClassVo.class);
                List<TYcodeClass> childrenList = ycodeClassService.getListByParentCode(vo.getCompleteCode());
                vo.setChildrenList(childrenList);
                result.add(vo);
            }
        }
        return Response.ok(result);
    }

    /**
     * 自定义分页
     */
    @GetMapping("/getProductPageList")
    @ApiOperation(value = "分页", notes = "传入tYcodeProduct")
    public Response<PageOutput<TYcodeProductListVo>> getProductPageList(TYcodeProductListDto listDto) {
        IPage<TYcodeProduct> page = tYcodeProductService.getPageList(listDto);
        List<TYcodeProductListVo> records = BeanUtil.copyList(page.getRecords(), TYcodeProductListVo.class);
        // 转换列表数据
        DataTransUtil.transListShowData(records);
        return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
    }

    @GetMapping("/getFirmName")
    @ApiOperation(value = "获取分类")
    public Response<List<Map>> getFirmName(String classId,String classCode) {
        return Response.ok(tYcodeProductService.getFirmName(classId,classCode));
    }

}
